<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="用户的id" prop="custId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.custId"-->
      <!--          placeholder="请输入用户的id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
<!--      <el-form-item label="维修编号" prop="maintenanceNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.maintenanceNo"-->
<!--          placeholder="请输入维修编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="一级区域名称" prop="regionId">
        <el-select
          v-model="queryParams.regionName"
          placeholder="请选择一级区域"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
        >
          <el-option
            v-for="item in regionOptions"
            :key="item.regionId"
            :label="item.label"
            :value="item.regionName"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="工人姓名" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入工人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="二级区域名称" prop="regionLevelTwoName">-->
<!--        <el-input-->
<!--          v-model="queryParams.regionLevelTwoName"-->
<!--          placeholder="二级区域名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="事件描述" prop="eventDescription">
        <el-input
          v-model="queryParams.eventDescription"
          placeholder="请输入事件描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="maintenanceState">
        <el-select
          v-model="queryParams.maintenanceState"
          placeholder="请选择状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in maintenanceStateOptions1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!--      <el-form-item label="维修日期" prop="maintenanceTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.maintenanceTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择维修日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="创建时间" prop="addTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.addTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择创建时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="addTime">
        <el-row>
          <el-col :span="11">
            <el-date-picker
              v-model="queryParams.startDate"
              type="date"
              placeholder="开始日期"
              value-format="yyyy-MM-dd"
              clearable
              style="width: 100%;"
            />
          </el-col>
          <el-col :span="2" class="text-center">
            <span>至</span>
          </el-col>
          <el-col :span="11">
            <el-date-picker
              v-model="queryParams.endDate"
              type="date"
              placeholder="结束日期"
              value-format="yyyy-MM-dd"
              clearable
              style="width: 100%;"
            />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->

      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->

      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->

      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">-->
    <!--      <el-table-column type="selection" width="55" align="center"/>-->
    <!--      <el-table-column label="维修编号" align="center" prop="maintenanceNo"/>-->
    <!--      <el-table-column label="工人姓名" align="center" prop="workerName"/>-->
    <!--      <el-table-column label="一级维修区域的名称" align="center" prop="regionId"/>-->
    <!--      <el-table-column label="二级维修区域的名称" align="center" prop="regionLevelTwoName"/>-->
    <!--      <el-table-column label="发包人" align="center" prop="fabaoren"/>-->
    <!--      <el-table-column label="事件描述" align="center" prop="eventDescription"/>-->
    <!--      <el-table-column label="签名" align="center" prop="isSign">-->
    <!--        <template slot-scope="scope">-->
    <!--          <span>{{ scope.row.isSign === 1 ? '已签名' : '未签名' }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="状态" align="center" prop="maintenanceState">-->
    <!--        <template slot-scope="scope">-->
    <!--    <span>{{-->
    <!--        scope.row.maintenanceState === 1 ? '草稿' :-->
    <!--          scope.row.maintenanceState === 2 ? '已提交' :-->
    <!--            scope.row.maintenanceState === 3 ? '已生成' : ''-->
    <!--      }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="工期的天数" align="center" prop="maintenanceNum"/>-->
    <!--      <el-table-column label="维修日期" align="center" prop="maintenanceTime" width="180">-->
    <!--        <template slot-scope="scope">-->
    <!--          <span>{{ parseTime(scope.row.maintenanceTime, '{y}-{m}-{d}') }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="创建时间" align="center" prop="addTime" width="180">-->
    <!--        <template slot-scope="scope">-->
    <!--          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-edit"-->
    <!--            @click="handleUpdate(scope.row)"-->
    <!--            v-hasPermi="['maintenance:maintenance:edit']"-->
    <!--          >修改-->
    <!--          </el-button>-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-delete"-->
    <!--            @click="handleDelete(scope.row)"-->
    <!--            v-hasPermi="['maintenance:maintenance:remove']"-->
    <!--          >删除-->
    <!--          </el-button>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--    </el-table>-->

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="维修编号" align="center" prop="maintenanceNo"/>
      <el-table-column label="工人姓名" align="center" prop="workerName"/>
      <el-table-column label="一级维修区域的名称" align="center" prop="regionName"/>
      <el-table-column label="二级维修区域的名称" align="center" prop="regionLevelTwoName"/>
      <el-table-column label="发包人" align="center" prop="fabaoren"/>
      <el-table-column label="事件描述" align="center" prop="eventDescription"/>
      <el-table-column label="签名" align="center" prop="isSign">
        <template slot-scope="scope">
          <span>{{ scope.row.isSign === 1 ? '已签名' : '未签名' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="maintenanceState">
        <template slot-scope="scope">
          <span>{{
              scope.row.maintenanceState === 1 ? '草稿' : scope.row.maintenanceState === 2 ? '已提交' : scope.row.maintenanceState === 3 ? '已生成' : ''
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工期天数" align="center" prop="maintenanceNum">
        <template slot-scope="scope">
          <span>{{ scope.row.maintenanceNum }}天</span>
        </template>
      </el-table-column>
      <el-table-column label="维修日期" align="center" prop="maintenanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建时间" align="center" prop="addTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!--      &lt;!&ndash; 图片展示 &ndash;&gt;-->
      <!--      <el-table-column label="图片" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div v-for="(pic, index) in scope.row.custMaintenancePics" :key="index">-->
      <!--            <img :src="pic.maintenancePic" alt="maintenance image" width="100" height="100" />-->
      <!--          </div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!-- 子项列表展示 -->
      <!--      <el-table-column label="材料" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <ul>-->
      <!--            <li v-for="(item, index) in scope.row.custMaintenanceList" :key="index" class="maintenance-list-item">-->
      <!--              {{ item.maintenanceListName }}-->
      <!--            </li>-->
      <!--          </ul>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改维修表列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="用户的id" prop="custId">-->
        <!--          <el-input v-model="form.custId" placeholder="请输入用户的id"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="维修编号" prop="maintenanceNo">
          <el-input v-model="form.maintenanceNo" placeholder="请输入维修编号"/>
        </el-form-item>
        <el-form-item label="工人姓名" prop="workerName">
          <el-input v-model="form.workerName" placeholder="请输入工人姓名"/>
        </el-form-item>
        <el-form-item label="一级区域名称" prop="regionName">
          <!--          <el-input v-model="form.regionName" placeholder="请输入一级维修区域的名称"/>-->
          <span>{{ form.regionName }}</span>
        </el-form-item>
        <el-form-item label="二级区域名称" prop="regionLevelTwoName">
          <el-input v-model="form.regionLevelTwoName" placeholder="请输入二级维修区域的名称"/>
        </el-form-item>
        <el-form-item label="发包人" prop="fabaoren">
          <el-input v-model="form.fabaoren" placeholder="请输入发包人"/>
        </el-form-item>
        <div v-if="form.voicePic" class="voice-player">
          <audio controls :src="form.voicePic">
            您的浏览器不支持 audio 标签。
          </audio>
        </div>
        <el-form-item label="事件描述" prop="eventDescription">
          <el-input v-model="form.eventDescription" type="textarea" placeholder="请输入内容"/>
        </el-form-item>


        <div v-if="form.custMaintenanceList" class="maintenance-list">
          <h3>材料列表</h3>
          <ul>
            <li v-for="item in form.custMaintenanceList" :key="item.maintenanceListId">
              {{ item.maintenanceListName }}
            </li>
          </ul>
        </div>
        <el-form-item label="签名状态" prop="isSign">
          <el-select v-model="form.isSign" placeholder="请选择签名状态">
            <el-option
              v-for="item in signOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="maintenanceState">
          <el-select v-model="form.maintenanceState" placeholder="请选择维修状态">
            <el-option
              v-for="item in maintenanceStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工期天数" prop="maintenanceNum">
          <el-input v-model="form.maintenanceNum" placeholder="请输入工期天数"/>
        </el-form-item>


        <div v-if="form.custMaintenancePics" class="image-gallery">
          <div v-for="(pic, index) in form.custMaintenancePics" :key="index" class="image-display">
            <p>{{ picTypes[pic.maintenancePicType] }}</p>
            <img :src="pic.maintenancePic" alt="Maintenance Picture"/>
          </div>
        </div>

        <el-form-item label="维修日期" prop="maintenanceTime">
          <el-date-picker clearable
                          v-model="form.maintenanceTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择维修日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
                          v-model="form.addTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  listMaintenance,
  getMaintenance,
  delMaintenance,
  addMaintenance,
  updateMaintenance, getName, getRegionsNameList
} from "@/api/maintenance/maintenance";
import axios from "axios";
export default {
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修表列表表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custId: null,
        maintenanceNo: null,
        workerName: null,
        regionId: null,
        regionLevelTwoName: null,
        fabaoren: null,
        eventDescription: null,
        isSign: null,
        maintenanceState: null,
        maintenanceNum: null,
        maintenanceTime: null,
        addTime: null,
        regionName: null,
        startDate: null,  // 开始日期
        endDate: null,    // 结束日期
      },
      regionOptions: [],
      signOptions: [
        {
          value: 1,
          label: '已签名'
        },
        {
          value: 2,
          label: '未签名'
        }
      ],
      maintenanceStateOptions: [
        {
          value: 1,
          label: '草稿'
        },
        {
          value: 2,
          label: '已提交'
        },
        {
          value: 3,
          label: '已生成'
        }
      ],
      maintenanceStateOptions1: [
        {
          value: 2,
          label: '已提交'
        },
        {
          value: 3,
          label: '已生成'
        }
      ],
      picTypes: {
        1: '原始照片',
        2: '竣工照片',
        3: '业主签字图'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };

  },
  created() {
    this.getList();
    this.loadRegionOptions();
  },
  methods: {
    /** 查询维修表列表列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    loadRegionOptions() {
      this.loading = true;
      getRegionsNameList().then(response => {
        this.regionOptions = response.rows;

        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        maintenanceId: null,
        custId: null,
        maintenanceNo: null,
        workerName: null,
        regionId: null,
        regionLevelTwoName: null,
        fabaoren: null,
        eventDescription: null,
        isSign: null,
        maintenanceState: null,
        maintenanceNum: null,
        maintenanceTime: null,
        updateTime: null,
        addTime: null,
        regionName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maintenanceId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修表列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const maintenanceId = row.maintenanceId || this.ids
      getMaintenance(maintenanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修表列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.maintenanceId != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maintenanceIds = row.maintenanceId || this.ids;
      this.$modal.confirm('是否确认删除维修表列表编号为"' + maintenanceIds + '"的数据项？').then(function () {
        return delMaintenance(maintenanceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleExport() {
      // 检查是否有选中的项
      if (this.ids.length === 0) {
        this.$modal.msgWarning("请先选择至少一项进行导出！");
        return; // 如果没有选中项，阻止继续执行
      }

      // 将选中的 maintenanceId 作为 URL 参数传递给后端
      const params = {
        maintenanceIds: this.ids.join(',')
      };

      // 假设 `download` 方法是通过 GET 请求发起请求
      // this.download('maintenance/maintenance/export', params, `maintenance_${new Date().getTime()}.xlsx`);
      // 假设 `download` 方法是通过 GET 请求发起请求
      this.downloadFile('/dev-api/maintenance/maintenance/export', params);
    },
    downloadFile(url, params) {
      // 使用 axios 或 fetch 发起请求
      axios({
        url: url,
        method: 'GET',
        params: params,
        responseType: 'blob'  // 确保返回的是文件
      })
        .then(response => {
          // 创建下载链接并触发下载
          const blob = response.data;
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement('a');
          a.href = url;
          a.download = `maintenance_${new Date().getTime()}.xlsx`;  // 设置下载文件名
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        })
        .catch(error => {
          console.error("下载文件失败", error);
          this.$modal.msgError("下载失败，请稍后再试");
        });
    }
  }
};
</script>

